#### Figure A3 and A4 Script ####
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(dplyr)


here::i_am("Scripts/FigA3A4Script.R")

library(here)

## Figure A3 ####
#First let's get the Michigan events:
kmow<-readRDS(here("Data", "analysis3_prelim.rds"))
              
              
allcc4a1<-kmow %>%
 filter(rcAMOUNT>0) %>%
 group_by(Candidate2Office2Year,cINCUMBENCYSTATUS)%>%
 summarise(p_ifr=sum(rcAMOUNT[fundraiser==1&Type_of_Contributor=="Individual"],na.rm=T)/sum(rcAMOUNT[Type_of_Contributor=="Individual"]),
           p_pac=sum(rcAMOUNT[Type_of_Contributor=="Non-Individual"&Broad_Sector_EDIT3%in%c("Party","Candidate Contributions")==F],na.rm=T)/sum(rcAMOUNT,na.rm=T),
           p_pac2=sum(rcAMOUNT[Type_of_Contributor=="Non-Individual"&Broad_Sector_EDIT3%in%c("Party","Candidate Contributions","Ideology/Single Issue")==F],na.rm=T)/sum(rcAMOUNT,na.rm=T),
           p_party=sum(rcAMOUNT[Broad_Sector_EDIT3=="Party"],na.rm=T)/sum(rcAMOUNT,na.rm=T),
           total=sum(rcAMOUNT,na.rm=T))


####Figure A3 ####

pdf(here("Results", "FigA3.pdf"),height=5,width = 10)
par(mfrow=c(1,1),mar=c(5,5,1,1))
boxplot(yaxt="n",ylab="% of Individual Donation Amounts From Events",main="",
       allcc4a1$p_ifr[allcc4a1$p_pac2<0.1&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.1&allcc4a1$p_pac2<0.2&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.2&allcc4a1$p_pac2<0.3&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.3&allcc4a1$p_pac2<0.4&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.4&allcc4a1$p_pac2<0.5&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.5&allcc4a1$p_pac2<0.6&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.6&allcc4a1$p_pac2<0.7&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.7&allcc4a1$p_pac2<0.8&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.8&allcc4a1$p_pac2<0.9&allcc4a1$total>=0],
       allcc4a1$p_ifr[allcc4a1$p_pac2>=0.9&allcc4a1$p_pac2<1&allcc4a1$total>=0],
       names=c("0-10","10-20","20-30","30-40","40-50","50-60","60-70","70-80","80-90","90-100"))
mtext("Percent of Donation Amounts From PACs",side=1,line=3)
axis(2,labels=c("0","20","40","60","80","100"),at=seq(0,1,0.2),las=1)
dev.off()

kmow<-subset(kmow,kmow$Broad_Sector_EDIT3%in%c("Party","Candidate Contributions")==F)
kmow$Candidate2Office2YearState<-paste(kmow$Candidate2Office2Year,kmow$cSTATE,sep="-")


####Figure A4####
#############################################

fourstates_a<-kmow %>%
 filter(cYearI%in%c(1,2)) %>%
 filter(Type_of_Contributor%in%c("Individual")) %>%
 filter(cOFFICE%in%c("HOUSE","SENATE","GOVERNOR")) %>%
 filter(ymdI%in%as.Date("0002-12-15")==F) %>% 
 group_by(cEYEAR,ymdI) %>%
 #group_by(cSTATE,cEYEAR,ymdI) %>%
 summarise(amount_fr=sum(rcAMOUNT[fundraiser==1],na.rm=T),amount_nfr=sum(rcAMOUNT[fundraiser==0],na.rm=T),
           pfr=sum(rcAMOUNT[fundraiser==1])/sum(rcAMOUNT)) %>%
 #group_by(ymdI,cSTATE) %>%
 group_by(ymdI) %>%
 mutate(med_pfr_ymdI=median(pfr,na.rm=T),avg_pfr_ymdI=mean(pfr,na.rm=T),
        log_median_fr_ymdI=log10(median(amount_fr,na.rm=T)),
        log_median_nfr_ymdI=log10(median(amount_nfr,na.rm=T)),
        log_mean_fr_ymdI=log10(mean(amount_fr,na.rm=T)),
        log_mean_nfr_ymdI=log10(mean(amount_nfr,na.rm=T)))
fourstates_a2<-unique(fourstates_a[,c("ymdI","med_pfr_ymdI","avg_pfr_ymdI","log_mean_fr_ymdI","log_mean_nfr_ymdI","log_median_fr_ymdI","log_median_nfr_ymdI")])
fourstates_a2<-fourstates_a2[order(fourstates_a2$ymdI),]


pdf(here("Results", "FigA4.pdf"),width=10)
plot(cex.lab=1.25,lty=1,as.Date(fourstates_a2$ymdI),fourstates_a2$log_median_fr_ymdI,ylim=c(3.5,7),type="l",lwd=2,ylab="Log (Base 10) of Monthly Median for Individual Donations",las=2,
    xaxt="n",xlab="Months Leading Up to/Following General Election")
lines(as.Date(fourstates_a2$ymdI),fourstates_a2$log_median_nfr_ymdI,col="darkgray",lwd=2,lty=2)
axis(side=1,labels=c("Year Before General Election","Year of General Election","General Election"),at=as.Date(c("0001-01-15","0002-01-15","0002-11-15")))
axis(side=1,labels=rep("",length(fourstates_a2$ymdI)),at=as.Date(fourstates_a2$ymdI))
abline(v=as.Date("0002-11-15"),lty=2,lwd=1)
abline(v=as.Date("0002-01-15"),lty=2,lwd=1)
legend(cex=1.25,lwd=2,legend=c("Event Donations","Non-Event Donations"),col=c("black","darkgrey"),"topleft",lty=c(1,2))
dev.off()
#############################################


#We can also pre-build the data set we ultimately use to conduct our third in-paper analysis

kmow3b<-kmow %>%
  filter(Type_of_Contributor%in%c("Individual")==T) %>%
  group_by(cINCUMBENCYSTATUS,quaTill_ge_date,cEYEAR,party3,cOFFICE,cOFFICEtype_sw_nogov,cSTATE,Candidate2Office2YearState,Candidate2Office2Year,cGENVOTESRECdummy) %>%
  summarise(fr=sum(rcAMOUNT[fundraiser==1],na.rm=T),nfr=sum(rcAMOUNT[fundraiser==0],na.rm=T),pfr=sum(rcAMOUNT[fundraiser==1],na.rm=T)/sum(rcAMOUNT,na.rm=T))


#get office types
kmow23<-unique(kmow[which(kmow$Type_of_Contributor=="Individual"),c("cOFFICE","Candidate2Office2YearState")])
kmow23$Office2i<-NA;for(i in 1:nrow(kmow23)){kmow23$Office2i[i]<-gregexpr(kmow23$cOFFICE[i],kmow23$Candidate2Office2YearState[i])[[1]][1]}
kmow23$Office2YearState<-"";for(i in 1:nrow(kmow23)){kmow23$Office2YearState[i]<-substr(kmow23$Candidate2Office2YearState[i],kmow23$Office2i[i],nchar(kmow23$Candidate2Office2YearState[i]))}
kmow4b<-merge(kmow3b,kmow23,by=c("Candidate2Office2YearState","cOFFICE"),all.x=T)
write.csv(kmow4b,here("Data", "analysis_data3.csv"))


